/*
  Exercise 9.2
  Create a function to return a value within a preset range from the potentiometer
  http://tronixstuff.com/tutorials > Chapter 9
*/

int readdial(int rangemax, int dialpin)
// rangemax is the number of values in your range, e.g. if you want 0~9, set rangemax to be '10'
// dialpin is the analog pin number connected to the potentiometer to read
{
  int kv=0;
  int kr=0;
  int kb=0;
  float a=0;
  float rd=0;
  rd=1023/rangemax;
  kb=analogRead(dialpin); // read potentiometer connected to analog pin 1
  a=kb/rd;
  kr=int(a);
  if (kr>rangemax)
  {
    kr=rangemax;
  }
  return kr;
}

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.write("Original knob value: ");
  Serial.print(analogRead(1));
  Serial.write(" adjusted range value: ");
  Serial.println(readdial(60,1));
}


